本次主題是以colab的環境進行學習的,在本篇文章中,我將講解影像辨識的基礎技能在接下來的文章中這些技能將多次出現,先讀過這些語法再繼續去看後面的文章會比較能快速上手喔。依照進度每個禮拜都會記錄不同的影像辨識方法,基本順序會從:
載入雲端硬碟:
from google.colab import drive
drive.mount('/content/drive')
下載mediapipe:
!pip install mediapipe
讀入照片及辨識:
import cv2 as cv
import mediapipe as med
import numpy as np
import matplotlib.pyplot as plt
dr = med.solutions.drawing_utils
dr_sty = med.solutions.drawing_styles
face = med.solutions.face_mesh
dr_face = dr.DrawingSpec(thickness=1, circle_radius=1)
cap = cv.imread('/content/drive/MyDrive/hand/image.jpg')
img = cap
with face.FaceMesh(
max_num_faces=1,
refine_landmarks=True,
min_detection_confidence=0.5,
min_tracking_confidence=0.5) as face_mesh:
img = cv2.resize(img,(1680,1920))
output = np.zeros((1920,1680,3), dtype='uint8')
img2 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = face_mesh.process(img2)
if results.multi_face_landmarks:
for face_landmarks in results.multi_face_landmarks:
dr.draw_landmarks(
image=output,
landmark_list=face_landmarks,
connections=face.FACEMESH_TESSELATION,
landmark_drawing_spec=None,
connection_drawing_spec=dr_sty
.get_default_face_mesh_tesselation_style())
dr.draw_landmarks(
image=output,
landmark_list=face_landmarks,
connections=face.FACEMESH_CONTOURS,
landmark_drawing_spec=None,
connection_drawing_spec=dr_sty
.get_default_face_mesh_contours_style())
dr.draw_landmarks(
image=output,
landmark_list=face_landmarks,
connections=face.FACEMESH_IRISES,
landmark_drawing_spec=None,
connection_drawing_spec=dr_sty
.get_default_face_mesh_iris_connections_style())
img = cv2.resize(img,(1680,1920))
img = cv.cvtColor(img,cv.COLOR_BGR2RGB)
plt.imshow(img,cmap="gray")
plt.show()
output = cv.cvtColor(output,cv.COLOR_BGR2RGB)
plt.imshow(output,cmap="gray")
plt.show()
原始圖片:
實際預測結果:
文章主題一覽:
粗體字為額外更新的文章。